﻿using System;
using System.ComponentModel;

namespace Epic.ComponentServices
{
    public class MergedPropertyOrderConverter : BasicPropertyOrderConverter
    {
#pragma warning disable 1591

        public TypeConverter BaseConverter { get; private set; }
        public MergedPropertyOrderConverter(TypeConverter baseConverter)
            : base()
        {
            BaseConverter = baseConverter;
        }

        //public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        //{
        //  return base.GetPropertiesSupported(context);
        //}
        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            if (BaseConverter.GetPropertiesSupported(context))
            {
                var result = BaseConverter.GetProperties(context, value, attributes);
                return BasicPropertyOrderConverter.Sort(result);
                // TODO: why!?
                //return result.Sort(SupportPropertyOrderConverter.ComparerInstance);
            }
            return base.GetProperties(context, value, attributes);
        }

        #region ensure original converter's functionality

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return BaseConverter.CanConvertFrom(context, sourceType);
        }
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            return BaseConverter.ConvertFrom(context, culture, value);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return BaseConverter.CanConvertTo(context, destinationType);
        }
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            return BaseConverter.ConvertTo(context, culture, value, destinationType);
        }

        public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
        {
            return BaseConverter.GetCreateInstanceSupported(context);
        }
        public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
        {
            return BaseConverter.CreateInstance(context, propertyValues);
        }

        // here!

        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return BaseConverter.GetStandardValuesSupported(context);
        }
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return BaseConverter.GetStandardValuesExclusive(context);
        }
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return BaseConverter.GetStandardValues(context);
        }

        public override bool IsValid(ITypeDescriptorContext context, object value)
        {
            return BaseConverter.IsValid(context, value);
        }

        #endregion
    }
}
